package com.leaderment.timatt.webmagic.pipeline;

import com.leaderment.timatt.SpringContextUtil;
import com.leaderment.timatt.mybatis.entity.TrackingData;
import com.leaderment.timatt.mybatis.mapper.CrawlerDomainProxyRedisMappingMapper;
import com.leaderment.timatt.service.impl.CrawlerServiceImpl;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

/** 
* @author hankJone
* @date 2018年3月20日
*/
public class MyConsolePipeline implements Pipeline {

    @SuppressWarnings("unused")
	@Override
    public void process(ResultItems resultItems, Task task) {

    	if(null == resultItems.getRequest())return;
		String theUrl = resultItems.getRequest().getUrl();
		
		if(null == resultItems.get("processStr") || null == resultItems.get("trackingId")) {
			CrawlerServiceImpl.oneSpider.stop();
			return;
		}
		
		TrackingData param = new TrackingData();
		
		param.setTrackingId(resultItems.get("trackingId"));
		param.setTrackingProcess(resultItems.get("processStr"));
		
		//该bean用于持久化爬下的数据.
		CrawlerDomainProxyRedisMappingMapper crawlerDomainProxyRedisMappingMapper=(CrawlerDomainProxyRedisMappingMapper)SpringContextUtil.getBean("crawlerDomainProxyRedisMappingMapper");
		
		crawlerDomainProxyRedisMappingMapper.updateTrackingProcess(param);
		
		if(null != CrawlerServiceImpl.oneSpider)CrawlerServiceImpl.oneSpider.stop();
	
    }
}
